﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;

namespace MaxLeaf.DAL
{
    public class ICPrcPlyEntryDALBuilder : BaseDALBuilder
    {


        public ICPrcPlyEntryDALBuilder()
            : base()
        {
            K3DAL = new K3.ICPrcPlyEntryDAL();

            WebDAL = new Web.mary_icprcplyentryDAL();

        }
        private bool CheckDtl(ServerInfo orgServer, string strFEntryID)
        {
            string strFInterID = string.Empty;
            using (DBAccess dbOrgCon = new DBAccess(orgServer))
            {
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPlyEntry", new Dictionary<string, object>() { { "FEntryID", strFEntryID } }, "FInterID,FItemID");
                if (drOrgData == null)
                {
                    LogManager.Debug(string.Format("ICPrcPlyEntry {0} not exist", strFEntryID));
                    return false;
                }
                strFInterID = CvtFunc.ToString(drOrgData["FInterID"]);

                if (! new ImInvMasterDALBuilder().CheckItem(dbOrgCon, CvtFunc.ToString(drOrgData["FItemID"])))
                {
                    return false;
                }


            }
            return new ICPrcPlyDALBuilder().CheckHdr(orgServer, strFInterID);
        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
            if (!orgServer.MasterDB)
            {
                //主账套分发
                return false;
            }

            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return true;
            }

            if (!CheckDtl(orgServer, CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue])))
            {
                return false;
            }

            return base.ValidCheckOrgData(orgServer, drvLog);
        }

        protected override bool ValidCheck(ServerInfo orgServer, ServerInfo destServer, DataRowView drvLog)
        {

            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return true;
            }

            string strFEntryID=CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue]);
            string strFInterID = string.Empty;
            using (DBAccess dbOrgCon = new DBAccess(orgServer))
            {
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPlyEntry", new Dictionary<string, object>() { { "FEntryID", strFEntryID } }, "FInterID,FItemID");
                if (drOrgData == null)
                {
                    LogManager.Debug(string.Format("ICPrcPlyEntry {0} not exist", strFEntryID));
                    return false;
                }
                strFInterID = CvtFunc.ToString(drOrgData["FInterID"]);
                
            }
            if( !new ICPrcPlyDALBuilder().CheckHdr(orgServer,destServer, strFInterID))
            {
                return false;
            }
            if (destServer.Type == ServerType.Web)
            {
                if (orgServer.CityDBs.Count == 0)
                {
                    using (DBAccess dbOrgCon = new DBAccess(orgServer))
                    {
                        DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPlyEntry", new Dictionary<string, object>() { { "FEntryID", drvLog[DEL.LogDEL.KeyValue] } }, "FInterID,FRelatedID");

                        DataRow drOrgFRelatedID = dbOrgCon.GetTableDataFirstRow("t_SubMessage", new Dictionary<string, object>() { { "FInterID", drOrgData["FRelatedID"] } }, "FName");
                        if (drOrgFRelatedID == null)
                        {
                            throw new MaxLeafException(string.Format("t_SubMessage 不存在 FInterID {0}", drOrgData["FRelatedID"]));
                        }
                        string strOrgFName = CvtFunc.ToString(drOrgFRelatedID["Fname"]);
                        //if (!strOrgFName.Equals("玛丽办公客户"))
                        //{
                        //    LogManager.Debug(string.Format("{0} 不分发网站", strOrgFName));
                        //    return false;
                        //}
                    }
                }
            }
            

            return base.ValidCheck(orgServer, destServer, drvLog);
        }

        protected override string GetK3DestServerDBName(ServerInfo orgServer, DataRowView drvLog)
        {
            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return base.GetK3DestServerDBName(orgServer, drvLog);
            }

            if (orgServer.Type == ServerType.K3 && orgServer.MasterDB && orgServer.CityDBs.Count > 0)
            {
                using (DBAccess dbOrgCon = new DBAccess(orgServer))
                {
                    DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("ICPrcPlyEntry", new Dictionary<string, object>() { { "FEntryID", drvLog[DEL.LogDEL.KeyValue] } }, "FInterID,FRelatedID");

                    DataRow drOrgFRelatedID = dbOrgCon.GetTableDataFirstRow("t_SubMessage", new Dictionary<string, object>() { { "FInterID", drOrgData["FRelatedID"] } }, "FName");
                    if (drOrgFRelatedID == null)
                    {
                        throw new MaxLeafException(string.Format("t_SubMessage 不存在 FInterID {0}", drOrgData["FRelatedID"]));
                    }
                    string strOrgFName = CvtFunc.ToString(drOrgFRelatedID["Fname"]);
                    if(true)// (!strOrgFName.Equals("玛丽办公客户"))
                    {
                        DataRow drDbName = dbOrgCon.GetTableDataFirstRow("U_CusTypeConDBname", new Dictionary<string, object>() { { "FTypeID", drOrgData["FRelatedID"] } }, "FDBName");
                        if (drDbName == null)
                        {
                            throw new MaxLeafException(string.Format("U_CusTypeConDBname 不存在 FTypeID {0}", drOrgData["FRelatedID"]));
                        }
                        string strDbName = CvtFunc.ToString(drDbName["FDBName"]);
                        if (strDbName.Length == 0)
                        {
                            throw new MaxLeafException(string.Format("U_CusTypeConDBname 不存在 DBName for FTypeID {0}", drOrgData["FRelatedID"]));
                        }

                        if (strDbName.Equals("mary", StringComparison.InvariantCultureIgnoreCase))
                        {
                            return orgServer.Database;
                        }

                        string strDestDBName = string.Empty;
                        foreach (string strCityDB in orgServer.CityDBs)
                        {
                            if (strDbName.Equals(strCityDB, StringComparison.InvariantCultureIgnoreCase))
                            {
                                strDestDBName = strCityDB;
                                break;
                            }
                        }
                        if (strDestDBName.Length == 0)
                        {
                            throw new MaxLeafException(string.Format("DBName {1} 无效 in U_CusTypeConDBname  for FTypeID {0}", drOrgData["FRelatedID"], strDbName));
                        }

                        return strDestDBName;
                    }
                }
            }

            return base.GetK3DestServerDBName(orgServer, drvLog);
        }
    }
}